package com.zhen.controller;

import com.zhen.domain.Book;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * User: zhen-desktop
 * Date: 2019/1/25
 * Time: 21:50
 */
@Controller
public class BookController {

    public static final Log LOGGER = LogFactory.getLog(BookController.class);

    @RequestMapping(value = "/sendxml",method = RequestMethod.POST)
    public void sendxml(@RequestBody Book book){
        LOGGER.info(book);
        LOGGER.info("接受XML数据成功");
    }

    @RequestMapping(value = "/readxml",method=RequestMethod.POST)
    public @ResponseBody Book readXml() throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Book.class);
        Unmarshaller unmar = context.createUnmarshaller();
        InputStream is = this.getClass().getResourceAsStream("/book.xml");
        Book book = (Book) unmar.unmarshal(is);
        LOGGER.info(book);
        return book;
    }
}
